'use strict';

const Controller = require('egg').Controller;

class LivespController extends Controller {
    async updateVideo() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                default: ''
            },
            videoname: {
                type: 'string',
                desc: '视频标题',
                default: ''
            },
            videourl: {
                type: 'string',
                required: true,
                desc: '视频路径',
                default: ''
            },
            imageurl: {
                type: 'string',
                allowNull: false,
                defaultValue: '',
                comment: '图片路径',
            },
            order: {
                type: 'string',
                required: true,
                desc: '视频简介',
            },
        });
        const { videoname, videourl, imageurl, order, id } = ctx.request.body;
        console.log(ctx.request.body, 123456);
        let Video = await app.model.Livesp.findOne({ id });
        if (!Video) {
            ctx.throw('更新失败,不存在此课程')
        };
        // swiper.image = image;
        // swiper.link = link;
        // swiper.order = order;
        // await swiper.save();
        await app.model.Livesp.update({ videoname, imageurl, videourl, order }, {
            where: {
                id
            }
        })
        ctx.apiSuccess('更新成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        let Video = await app.model.Livesp.findOne({
            where: {
                id: id
            }
        });
        ctx.apiSuccess(Video);
    }

    async deleteCourses() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '视频ids'
            }
        });
        let { ids } = ctx.request.body;
        ids = JSON.parse(ids);
        await app.model.Livesp.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async livespList() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;
        // const id = ctx.query.id;
        // if (!id) {
        //     ctx.throw('课程id为空')
        // };
        let course = await app.model.Livesp.findAll();
        // let data = await ctx.page('Livesp')
        // console.log(JSON.parse(JSON.stringify(course)));

        // if (!course) {
        //     return ctx.apiSuccess({
        //         totalCount: 0,
        //         currPage: pageNumber,
        //         list: []
        //     });
        // };
        let list = course.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);

        // 返回用户信息和token
        ctx.apiSuccess({
            totalCount: course.length,
            currPage: pageNumber,
            list: JSON.parse(JSON.stringify(list))
        });

    }
    async addVideo() {
        const { ctx, app } = this;
        ctx.validate({
            videoname: {
                type: 'string',
                desc: '视频标题',
                default: ''
            },
            videourl: {
                type: 'string',
                required: true,
                desc: '视频路径',
                default: ''
            },
            imageurl: {
                type: 'string',
                allowNull: false,
                defaultValue: '',
                comment: '图片路径',
            },
            order: {
                type: 'string',
                required: true,
                desc: '视频简介',
            },
        });
        const { videoname, videourl, imageurl, order } = ctx.request.body;
        const data = await app.model.Livesp.create({
            videoname,
            videourl,
            imageurl,
            order
        });
        console.log(data);
        // ctx.apiSuccess('添加成功')
    };
}

module.exports = LivespController;